package com.blackline.auto.Report;

import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author Allen
 * @version May 24, 2013
 *          <p>
 *          This is to run all 17 standard reports with all 14 roles.
 */
public class Reports_Allens_Ones
{

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger = Logger.getLogger(Reports_Allens_Ones.class);
	UI UI = new UI();
	Parameters parameters;

	int sleepTime = 2000;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
	Date date = new Date();
	String dateString = sdf.format(date);

	TestData testdata = new TestData("TestData/RunStdReportsWithAllRoles.xml");

	String t2Test2 = testdata.getInput("t2Test2");
	String testStarter = testdata.getInput("testStarter");
	String testInternal2 = testdata.getInput("testInternal2");
	String user = testdata.getInput("user");
	String password = testdata.getInput("password");

	String[] roles =
	{ 		"Preparer",
			"Approver",
			"Reviewer", 
			"Financial Reviewer",
			"Account Reviewer", 
			"Financial Manager",
			"Account Manager",
			"Auditor (Acct View)", 
			"Executive",
			"CFO", 
			"Auditor (Entity View)",
			"Local Admin", 
			"Business Admin",
			"System Admin"
			};

	String[] standardReports =
	{ 
			"User Role Assignments", 
			"Control Analysis Past Due By Entity",
			"Account Reconciliation Summary with Open Items", 
			"Control Analysis with Detail", 
			"Account Reconciliation Summary",
			"Account Reconciliation Required Adjustments", 
			"Account Reconciliation Open Items",
			"Unassigned Accounts",
			"Uploaded Account Information",
			"Company Policies and Reconciliation Procedures", 
			"User Access",
			"Certification Log", 
			"Status with Comment Information",
			"Certification Status By People",
			"Certification Status By Entity",
			"Certification Status By Entity For Period Range", 
			"Assignment Status" 
	};

	String desc_string_for_Account_Reconciliation_Summary = "category totals";
	String desc_string_for_Certification_Status_By_Entity = "Summary report";

	@Before
	public void setUp() throws Exception
	{
		FirefoxProfile profile = new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters = new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}

	@Test
	public void RunWithAllRolesTestStarter() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + testStarter + " with: " + user);
		UI.LogonPage.Login(driver, testStarter, user, password);
		
		// Start run the report with different roles.
		runStdReportsWithSpecificRole(1, testStarter);
		
		runStdReportsWithSpecificRole(2, testStarter);
		
		runStdReportsWithSpecificRole(3, testStarter);
		
		runStdReportsWithSpecificRole(4, testStarter);
		
		runStdReportsWithSpecificRole(5, testStarter);
		
		runStdReportsWithSpecificRole(6, testStarter);
		
		runStdReportsWithSpecificRole(7, testStarter);
		
		runStdReportsWithSpecificRole(8, testStarter);
		
		runStdReportsWithSpecificRole(9, testStarter);
		
		runStdReportsWithSpecificRole(10, testStarter);
		
		runStdReportsWithSpecificRole(11, testStarter);
		
		runStdReportsWithSpecificRole(12, testStarter);
		
		runStdReportsWithSpecificRole(13, testStarter);
		
		runStdReportsWithSpecificRole(14, testStarter);
		
		runStdReportsWithSpecificRole(15, testStarter);
		
	}
	
	
	
	@After
	public void tearDown() throws Exception
	{
		// CommonMethods.takeScreenShot(this.getClass(), driver);
		// driver.close();
		// driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	/**
	 * This is to run Allen's 3 standard reports with specific roles
	 * 
	 * @param roleName
	 *            The name of the role to run the reports
	 */
	private void runStdReportsWithSpecificRole(int sequence, String site) throws Exception
	{
		// Switch to different roles
		UI.SessionMenu.switchRoleBaseOnSequence(sequence);

		// Go to Enhanced Reporting page
		Thread.sleep(sleepTime);
		driver.get(site);
		Thread.sleep(sleepTime);

		// ======== Go to Accounts node========
		logger.info("=====Accounts Node=====");
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(sleepTime);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(sleepTime);

		// Run User Role Assignment
		logger.info("Going to run: " + standardReports[0]);
		UI.Reporting.Name_text_field.clear();
		UI.Reporting.Description_text_field.clear();
		UI.Reporting.Name_text_field.clear();
		
		UI.Reporting.Name_text_field.sendKeys(standardReports[0]);
		UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
		Thread.sleep(sleepTime);

		UI.Reporting.Run_linkOfSearchedResult.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Run_Report_button.click();
		Thread.sleep(sleepTime);
		UI.Reporting.OK_button.click();
		Thread.sleep(sleepTime);


		// ======== Run Unassigned Accounts report under Information node.========
		logger.info("=====Information Node=====");
		UI.Reporting.Information_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		for (int i = 7; i < 8; i++)
		{
			logger.info("Going to run: " + standardReports[i]);
			UI.Reporting.Name_text_field.clear();
			UI.Reporting.Description_text_field.clear();
			UI.Reporting.Name_text_field.clear();
			
			UI.Reporting.Name_text_field.sendKeys(standardReports[i]);
			UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(sleepTime);

			UI.Reporting.Run_linkOfSearchedResult.click();
			Thread.sleep(sleepTime);
			UI.Reporting.Run_Report_button.click();
			Thread.sleep(sleepTime);
			UI.Reporting.OK_button.click();
			Thread.sleep(sleepTime);
		}

		// ======== Run Assignment Status under Status node.========
		logger.info("=====Status Node=====");
		UI.Reporting.Status_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		for (int i = 16; i < 17; i++)
		{
			logger.info("Going to run: " + standardReports[i]);
			UI.Reporting.Name_text_field.clear();
			UI.Reporting.Description_text_field.clear();
			UI.Reporting.Name_text_field.clear();

			if (standardReports[i].equals("Certification Status By Entity"))
			{
				UI.Reporting.Description_text_field.sendKeys(desc_string_for_Certification_Status_By_Entity);
				Thread.sleep(sleepTime);
			}

			UI.Reporting.Name_text_field.sendKeys(standardReports[i]);
			UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(sleepTime);

			UI.Reporting.Run_linkOfSearchedResult.click();
			Thread.sleep(sleepTime);
			UI.Reporting.Run_Report_button.click();
			Thread.sleep(sleepTime);
			UI.Reporting.OK_button.click();
			Thread.sleep(sleepTime);
		}

		logger.info("==========Current time is: " + dateString + "; Role " + sequence + " done.==========");

	}


}
